package control_centre

import (
	"github.com/hajimehoshi/ebiten/v2"
	"study/PlantsVsZombies1/utils"
	"time"
)

type MouseState int         // 鼠标状态
type DragPlantCardState int // 拖拽植物卡片状态
type GameCenter int         // 游戏中心
type PlantSituation int     // 植物状态
type JudgeCollision int     // 碰撞状态
type LiftState int          // 僵尸生命状态
type zombieEat int          // 僵尸吃植物

const (
	Deactivation DragPlantCardState = iota
	Activate
)
const (
	ReleaseTheMouse MouseState = iota
	ClickTheMouse
	DragTheMouse
)
const (
	AdventureEntrance GameCenter = iota
	AdventureWar
)
const (
	secure PlantSituation = iota
	Danger
)
const (
	NoCollision JudgeCollision = iota
	Collision
)
const (
	live LiftState = iota
	Dead
	BombDead
)
const (
	NoEat zombieEat = iota
	IsEat
)

type Game struct {
	Config                  *Config
	MouseState              MouseState
	dragStartX, dragStartY  int
	DragPlantCardState      DragPlantCardState
	staticName              *StaticVariable
	GameCenter              GameCenter
	GameCenterEntranceState DragPlantCardState
}

type GameInterface interface {
	Draw(screen *ebiten.Image)
	Layout(outerWidth, outerHeight int) (screenWidth, screenHeight int)
	Update() error
	GetMouseState()
}

func (g *Game) Update() error { //TODO implement me
	// 判断鼠标状态
	g.GetMouseState()

	if g.GameCenter == AdventureEntrance { // 冒险菜单
		//utils.GetCoordination()

	} else if g.GameCenter == AdventureWar { // 冒险战斗
		// 检测监视是否出现，并改变植物状态
		ScanLawnZombie(&ZombieExistence)
		// PrintlnZombieScan(&ZombieExistence) // 检测僵尸出没
		utils.GetTurfGrid(utils.GetCoordination())
		// 更新每个植物状态
		UpdatePlantState(&ZombieExistence) // 添加豌豆
		ShootPea()                         //创建豌豆
		// 删除出界豌豆
		DeletePeaOverWall()
		// 加测僵尸是否在吃植物
		ScanZombiePlantCollision()

		// 检测碰撞
		CollisionZombie()
		// 加测僵尸是否死亡
		ScanZombieDead()

	}

	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	//TODO implement me
	if g.GameCenter == AdventureEntrance { // 冒险菜单
		// 展示主菜单
		DisplayAdventureEntrance(screen)
		//DisplayNoEntrance(screen)
		// 鼠标状态
		g.DisplayMouseClickMode(screen)
	} else if g.GameCenter == AdventureWar { // 冒险战斗
		// 战斗背景
		BackGroundDraw(screen)
		// 植物卡片
		CardSlotDraw(screen)
		PlantCardDraw(screen)
		// 种植植物
		g.DisplayPlantImage(screen)

		// 绘制僵尸
		g.DisplayZombieImage(screen)
		DrawPeaAttack(screen)

		// 拖拽植物卡片，将植物种植
		g.GrowPlant(screen)
	}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	//TODO implement me
	return g.Config.ScreenWidth, g.Config.ScreenHeight
}

func NewGame() *Game {
	// 设计窗口
	ebiten.SetWindowSize(cfgInit.ScreenWidth, cfgInit.ScreenHeight)
	ebiten.SetWindowTitle(cfgInit.Title)
	// 返回Game
	var g Game
	g.Config = cfgInit
	g.staticName = &StaticVariable{}      // 因为传递为指针，故需初始化
	g.GameCenter = AdventureWar           // 游戏开始页面
	EnterTheArenaPreheatTime = time.Now() // 一会就删掉
	ZombieToggleMovement = time.Now()
	peaShootTime = time.Now()
	g.GameCenterEntranceState = Deactivation // 为-未点击开始游戏的状态
	return &g
}
func (g *Game) GetMouseState() {
	x, y := ebiten.CursorPosition()
	mousePressed := ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
	// 逻辑缜密的改动
	switch g.MouseState {
	case ClickTheMouse:
		if mousePressed {
			if g.dragStartX != x || g.dragStartY != y {
				g.MouseState = DragTheMouse
			}
		} else {
			g.MouseState = ReleaseTheMouse
		}
	case DragTheMouse:
		if !mousePressed {
			g.MouseState = ReleaseTheMouse
		}

	case ReleaseTheMouse:
		if mousePressed {
			g.MouseState = ClickTheMouse
			g.dragStartY = y
			g.dragStartX = x
		}
	}

	// 旧版本
	//if !mousePressed && (g.MouseState == DragTheMouse || g.MouseState == ClickTheMouse) {
	//	g.MouseState = ReleaseTheMouse
	//} else if mousePressed && g.dragStartY != y && g.dragStartX != x {
	//	g.MouseState = DragTheMouse
	//} else if mousePressed {
	//	g.MouseState = ClickTheMouse
	//}

	//// 判断鼠标状态
	//switch g.MouseState {
	//case ClickTheMouse:
	//	fmt.Println("ClickTheMouse")
	//case DragTheMouse:
	//	fmt.Println("DragTheMouse")
	//case ReleaseTheMouse:
	//	fmt.Println("ReleaseTheMouse")
	//}
}
